package com.longli.tank;

import com.longli.tank.strategy.*;

import java.awt.*;
import java.awt.event.KeyEvent;

public class Player extends AbstractGameObject{
    private int x,y;
    private final  int SPEED=5;
    private Dir dir;
    private boolean moving;
    private boolean bL,bR,bU,bD;
    private Group group;
    private boolean live=true;

    public Player(int x, int y, Dir dir, Group group) {
        this.x=x;
        this.y=y;
        this.dir=dir;
        this.group=group;

    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    @Override
    public void paint(Graphics g){
            switch (dir) {
                case L:
                    g.drawImage(ResourceMgr.goodTankL, x, y, null);
                    break;
                case R:
                    g.drawImage(ResourceMgr.goodTankR, x, y, null);
                    break;
                case U:
                    g.drawImage(ResourceMgr.goodTankU, x, y, null);
                    break;
                case D:
                    g.drawImage(ResourceMgr.goodTankD, x, y, null);
                    break;
            }
        move();
      }


    public void keyPassed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode){
            case KeyEvent.VK_LEFT:
                bL=true;
                break;
            case KeyEvent.VK_RIGHT:
                bR=true;
                break;
            case KeyEvent.VK_UP:
                bU=true;
                break;
            case KeyEvent.VK_DOWN:
                bD=true;
                break;
        }
        setMainDir();

    }

    private void move() {
        if(!moving){return;}
        switch (dir){
            case L:
                x-=SPEED;
                break;
            case R:
                x+=SPEED;
                break;
            case U:
                y-=SPEED;
                break;
            case D:
                y+=SPEED;
                break;
        }

    }

    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode){
            case KeyEvent.VK_LEFT:
                bL=false;
                break;
            case KeyEvent.VK_RIGHT:
                bR=false;
                break;
            case KeyEvent.VK_UP:
                bU=false;
                break;
            case KeyEvent.VK_DOWN:
                bD=false;
                break;
            case KeyEvent.VK_CONTROL:
                this.fire();
                break;
        }
        setMainDir();

    }

    private void setMainDir() {
        if(!bR && !bL && !bU&& !bD){
            moving=false;
        }else {
            moving=true;
            if(bR && !bL && !bU&& !bD){
                dir=Dir.R;
            }
            if(!bR && bL && !bU&& !bD){
                dir=Dir.L;
            }
            if(!bR && !bL && bU&& !bD){
                dir=Dir.U;
            }
            if(!bR && !bL && !bU&& bD){
                dir=Dir.D;
            }
        }

    }

    public void fire(){
        FireStrategy fireStrategy=null;
        try {
            //ClassLoader loader=Player.class.getClassLoader();
            //Class clazz = loader.loadClass("com.longli.tank.strategy" + "."+PropertyMgr.get("tankFireStrategy"));
            Class clazz=Class.forName("com.longli.tank.strategy." +PropertyMgr.get("tankFireStrategy"));
            fireStrategy= (FireStrategy) clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        fireStrategy.fire(this);
    }
    public void die(){
        this.setLive(false);
    }

}
